/**
 * 
 */
package com.door.erp.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Client;
import com.door.erp.model.Employee;
import com.door.erp.model.Product;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.EmployeeService;

/**
 * @author xiaolongwei@gmail.com
 *
 * 
 */
@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
	@Resource(name="employeeService", type=EmployeeService.class)
	private EmployeeService  employeeService;
	@RequestMapping(value = "/save.do")
	public ModelAndView save(Employee employee,@RequestParam("file")   CommonsMultipartFile  file,HttpServletRequest request) {
		File Imgdir = new File(request.getServletContext().getRealPath("/attachment/employee"));		
		employee.setPassword(employeeService.generatePwd());
		String fileName = employeeService.saveEmployeeImg(file, Imgdir);
		employee.setHeadPic("/attachment/employee/"+fileName);
		employee.setStatus(BaseInfoStatus.NORMAL);
		employeeService.add(employee);
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("employee");
		ModelAndView mv = new ModelAndView("dwzresponse");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/edit/{id}.do")
	public  ModelAndView  edit(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("employee/edit");
		Employee employee = employeeService.getObject(id);
		mv.addObject("employee", employee);
		return mv;
	}
	@RequestMapping(value = "/add.do")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView("employee/add");	
		return mv;
	}
	@RequestMapping(value = "/index.do")
	public ModelAndView index(Pager pager) {
		ModelAndView mv = new ModelAndView("employee/employeeIndex");	
		List<Employee> employees = employeeService.getAll(new HashMap(), pager);
		mv.addObject("employees", employees);
		mv.addObject("pager", pager);
		return mv;
	}
	@RequestMapping(value = "/update.do")
	public ModelAndView update(Employee employee ,@RequestParam("file")   CommonsMultipartFile  file,HttpServletRequest request) {
		Employee e = employeeService.getObject(employee.getId());
		if(file==null){
			employee.setHeadPic(e.getHeadPic());
		}
		else{
			File Imgdir = new File(request.getServletContext().getRealPath("/attachment/employee"));		
			
			String fileName = employeeService.saveEmployeeImg(file, Imgdir);
			employee.setHeadPic("/attachment/employee/"+fileName);
		}
		employee.setStatus(e.getStatus());
		employee.setPassword(e.getPassword());
		employeeService.modify(employee);
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("employee");
		ModelAndView mv = new ModelAndView("dwzresponse");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/delete/{id}.do")
	public ModelAndView delete(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("dwzresponse");
		Employee e = employeeService.getObject(id);
		e.setStatus(BaseInfoStatus.DELETED);
		employeeService.modify(e);
	//	mv.addObject("stock", StockManager.pop());
		DwzResponse resonse = new DwzResponse();		
		resonse.setNavTabId("employee");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/find/{id}.do")
	public ModelAndView find(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("employee/detail");
		Employee employee= employeeService.getObject(id);
		mv.addObject("employee", employee);
		return mv;
	}
	@RequestMapping(value = "/lookuplist.do")
	public ModelAndView lookup(Pager pager) {
		ModelAndView mv = new ModelAndView("employee/employee_lookup");
		List<Employee> employees= employeeService.getAll(new HashMap(), pager);
		mv.addObject("employees", employees);
		return mv;
	}
}
